
import os.path
import math

def convert_line(line):
    line = line.replace('\t', '').replace('(', ',').replace(')', '').replace('\xb0', '').replace('dB', '').replace('\n', '')
    vals = line.split(',')
    vals[1] = '{:e}'.format(math.pow(10, float(vals[1]) / 20))
    vals[2] = '{:e}'.format(float(vals[2]) * math.pi / 180)
    return ';'.join(vals)
    
def convert_file(filename):
    parts = os.path.splitext(filename)
    csvname = parts[0] + '.csv';
    with open(filename, 'r') as fin:
        with open(csvname, 'w') as fout:
            fout.write('"Freq";"Real";"Imag"\n')
            fin.readline();
            for line in fin:
                fout.write(convert_line(line) + '\n')

def main():
    files = ['a500 #1 filter off.txt', 'a500 #1 filter on.txt', 
             'a1200 #1 filter off.txt', 'a1200 #1 filter on.txt']
    for f in files:
        convert_file(f)

if __name__ == '__main__':
    main()